package com.rain.jwt.common;

import lombok.Data;
import lombok.NoArgsConstructor;

import static com.rain.jwt.common.ResultEnum.FAIL;
import static com.rain.jwt.common.ResultEnum.SUCCESS;

/**
 * @author: wu
 * @date: 2021/9/25
 */
@Data
@NoArgsConstructor
public class Result<T> {
    private int code;
    private String msg;
    private T data;

    public Result(int code, String msg, T data) {
        this.code = code;
        this.msg = msg;
        this.data = data;
    }

    public Result(ResultEnum resultEnum) {
        this.code = resultEnum.getCode();
        this.msg = resultEnum.getMsg();
    }

    public Result(int code, String msg) {
        this.code = code;
        this.msg = msg;
    }

    public static <T> Result<T> success(T t) {
        Result result = new Result(SUCCESS);
        result.setData(t);
        return result;
    }

    public static Result fail(String msg) {
        Result result = new Result(FAIL);
        result.setMsg(msg);
        return result;
    }

    public static Result error(BizException e) {
        Result result = new Result(e.getCode(), e.getMsg());
        return result;
    }

    public static Result error(Exception e) {
        Result result = new Result(FAIL);
        result.setMsg(e.getMessage());
        return result;
    }

}
